<?php

namespace frontend\controllers;

use common\models\Product;
use common\models\User;
use Yii;
use yii\helpers\Url;
use yii\web\Controller;
use yii\filters\AccessControl;

class UiController extends Controller
{
    public $layout = 'main-ui';
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['index','fast-index','login', 'logout', 'signup'],
//                'only' => ['index','login', 'logout', 'signup'],
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => ['login', 'signup'],
                        'roles' => ['?'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['index','fast-index','logout'],
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }

    /**
     * 慢速版的选商品页面（每次点击都会进行数据库操作，实现购物车的实时读写）
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionFastIndex()
    {
        return $this->render('fast-index');
    }

    /**
     * @param $action
     * @return bool
     * @throws \yii\web\BadRequestHttpException
     */
    public function beforeAction($action)
    {
        //设置登录后跳转URL
        if(Yii::$app->user->isGuest)
            Yii::$app->session->set('redirectUrl',Yii::$app->request->url);
        $user = User::findOne(12);
        Yii::$app->user->login($user);
        return parent::beforeAction($action); // TODO: Change the autogenerated stub
    }
}
